package com.rh.bmw4s.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rh.bmw4s.R;
import com.rh.bmw4s.adapter.FilterAdapter;
import com.rh.bmw4s.bean.BaseBean;
import com.rh.bmw4s.bean.BrandBean;
import com.rh.bmw4s.bean.FilterBean;
import com.rh.bmw4s.bean.MyHashMap;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.utills.DealResult;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FilterActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.tv_brand)
    TextView tvBrand;

    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private FilterAdapter mAdapter;
    private HashMap<String, String> paraMaps;
    private int brandId = 0;
    private int imFrom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        OkGo.<String>post(Constant.FILTER)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        BaseBean<FilterBean> bean = DealResult.getInstace().dealData(FilterActivity.this, response, new TypeToken<BaseBean<FilterBean>>() {
                        }.getType());
                        if (bean == null) {
                            return;
                        }
                        List<FilterBean> beans = bean.getDatas();
                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(FilterActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<FilterBean> beans) {
        mAdapter.addAll(beans);
        recyclerView.refresh();
    }

    private void initView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            //新车只有宝马，所以跳转过来要判断跳到那个界面
            imFrom = bundle.getInt("imfrom");
        }
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        titleRightOneTv.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("高级筛选");
        titleRightOneTv.setText("重置");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new FilterAdapter(FilterActivity.this);
        mAdapter.setDataList(new ArrayList<FilterBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(FilterActivity.this)
                .setHeight(R.dimen.divider_height_4)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_1)
                .build();
        recyclerView.addItemDecoration(divider);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.refresh();
    }

    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv, R.id.tv_brand, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_tv:
//                重置
                mAdapter.clearMaps();
                List<FilterBean> lists = mAdapter.getDataList();
                for (FilterBean bean : lists) {
                    bean.setNum2("");
                    bean.setNum1("");
                }
                tvBrand.setText("不限品牌");
                lRecyclerViewAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_brand:
                if (imFrom == 22) {
                    startActivityForResult(new Intent(FilterActivity.this, NewCarSelectTypeActivity.class), 0);
                } else {
                    startActivityForResult(new Intent(FilterActivity.this, CategoryActivity.class), 0);
                }

                break;
            case R.id.tv_confirm:
                MyHashMap maps = new MyHashMap();
                paraMaps = mAdapter.getParaMaps();
                if (brandId == 0) {
                    paraMaps.put("品牌", "");
                } else {
                    paraMaps.put("品牌", brandId + "");
                }
                maps.setMap(paraMaps);
                Bundle b = new Bundle();
                b.putSerializable("maps", maps);
                Intent i = new Intent();
                i.putExtras(b);
                this.setResult(Constant.SUCESS_FILTRE, i);
                this.finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Constant.SUCESS_BRAND) {
            BrandBean.BrandsBean bean = (BrandBean.BrandsBean) data.getSerializableExtra("bean");
            tvBrand.setText(bean.getModel_name());
            brandId = bean.getId();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
